package com.tanhua.manager.listener;

import com.tanhua.commons.template.ContentCensorTemplate;
import com.tanhua.domain.mogo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "tanhua-public-group",topic = "tanhua-publish")
public class PublishCensorMessageListener implements RocketMQListener<String> {
    @Reference
    private PublishApi publishApi;
    @Autowired
    private ContentCensorTemplate contentCensorTemplate;

    @Override
    public void onMessage(String message) {
        log.info("========开始审核动态内容，动态id：{}",message);
       //查询动态
        Publish publish = publishApi.findById(message);
        //审核文本内容
        boolean textCensor = contentCensorTemplate.textCensor(publish.getTextContent());
        //审核图片内容
        boolean imageOk = contentCensorTemplate.imageBatchCensor(publish.getMedias());
        //更新审核结果
        publish.setState(textCensor&&imageOk ? 1:0);
        publishApi.updateState(publish);
        log.info("====审核动态完成，动态Id：{}",message,textCensor&&imageOk);

    }
}
